import React from 'react';
import { Route } from 'react-router-dom';
import { hasToken } from 'utils/strorage';
import { Redirect } from 'react-router-dom';

// 4. 重构到单独的组件中维护AuthRoute

// 1. 定义AuthRoute组件， 将Route的逻辑迁移到AuthRoute内
export default class AuthRoute extends React.Component {
  render() {
    //属性属性： ...restProps表示除了已解构的属性，剩下的属性存在restProps对象上
    // 1. 剩余剩余属性...restProps
    const { path, component: Component, ...restProps } = this.props;
    return (
      <Route
        {...restProps}
        path={path}
        render={(_props) => {
          if (hasToken()) {
            // 3.通过props传值， 复用component
            return <Component {..._props} />;
          }
          return <Redirect to="/login" />;
        }}
      ></Route>
    );
  }
}
